﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using T.FAS.Runtime.DataAccess.SPI;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Base.Entity;
using System.Data;
//using T.FAS.Authority.Metadata.Entity;
//using T.FAS.Authority.MetaData.Dev.Plugin;

namespace T.FAS.Runtime.DataAccess.Engine
{
    class DQQueryServiceProxy : IModelQueryService<DataQuery>
    {
        public event ExecuteDataAccessAction Execute;
        public event BuildDataAccessActionContext BuildContext;

        public string GetQueryString(DataQuery dq, Dictionary<string, object> extParam = null)
        {
            return DQQueryServiceFactory.GetDQQueryService(dq).GetQueryString(dq, extParam);
        }

        public TResult Query(DataQuery dq, Dictionary<string, object> parameters, Dictionary<string, object> extParam = null)
        {
            return DQQueryServiceFactory.GetDQQueryService(dq).Query(dq, parameters, extParam);
        }
    }

    class DQQueryServiceFactory
    {
        public static DatabaseType[] s_bigDataDBType = new DatabaseType[] 
        { 
            DatabaseType.ES, 
            DatabaseType.StarRocks, 
            DatabaseType.TiDB,
            DatabaseType.Druid,
            DatabaseType.HBase,
        };
        public static Dictionary<string, IModelQueryService<DataQuery>> s_serviceCache = new Dictionary<string, IModelQueryService<DataQuery>>
        {
            {"Default", new DQQueryService()},
            {"BigData", new BigDataDQQueryService()},
        };

        public static IModelQueryService<DataQuery> GetDQQueryService(DataQuery dq)
        {
            if (dq == null)
            {
                return s_serviceCache["Default"];
            }
            else
            {
                if (dq.IsBigDataQuerySource())
                {
                    return s_serviceCache["BigData"];
                }
                else
                {
                    return s_serviceCache["Default"];
                }
            }
        }
    }
}
